/**
 * 作者: lby
 * 日期: 2024-11-06 20:29:08
 * 备注: 玩家约会表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserDating;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserDatingModel extends BaseModel implements Model {
	public static final String LISTKEY = "";
	public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
	public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
	public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

	protected long id; // userId
	protected int strength; // 精力
	protected long recoverTime; // 上次精力开始恢复时间
	protected int dateNum; // 普通约会的次数

	public void reset() {
	}

	protected String getKey() {
		return "UserDating." + id;
	}

	protected String getListKey() {
		return null;
	}

	protected String getListItemKey() {
		return null;
	}

	public void doCreate() {
		setTableId();
		insert();
	}

	public void setTableId() {
		putId(incrTableId("user_dating"));
	}

	public JsonObject insert() {
		return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
	}

	public void delete() {
		super.delete(getKey(), id, UPDATE_DB_TYPE, id, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
	}

	/**
	 * 根据入库类型更新
	 */
	public JsonObject update() {
		return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, id, getAllObj(), LISTKEY, getListKey(), getListItemKey());
	}

	/**
	 * 实时入队列
	 */
	public JsonObject updateQueueNow() {
		return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
	}

	public JsonObject getAllObj(){
		JsonObject obj = new JsonObject();
		obj.put("id", id); // userId
		obj.put("strength", strength); // 精力
		obj.put("recoverTime", recoverTime); // 上次精力开始恢复时间
		obj.put("dateNum", dateNum); // 普通约会的次数
		return obj;
	}

	public PBUserDating.Builder takeInitBuilder() {
		PBUserDating.Builder sb = PBUserDating.newBuilder();
		if (strength != 0) {
			sb.setStrength(strength);
		}
		if (recoverTime != 0) {
			sb.setRecoverTime(recoverTime);
		}
		return sb;
	}

	public PBUserDating.Builder takeBuilder() {
		if (updateObj == null || updateObj.isEmpty()) {
			return takeInitBuilder();
		}
		PBUserDating.Builder sb = PBUserDating.newBuilder();
		if (isFieldUpdate("strength")) {
			sb.setStrength(strength);
		}
		if (isFieldUpdate("recoverTime")) {
			sb.setRecoverTime(recoverTime);
		}
		return sb;
	}

	public long getId() {
		return id;
	}

	public void putId(long id) {
		if (this.id != id) {
			this.id = id;
			updateRedisHashField(getKey(), "id", this.id);
		}
	}

	public void addId(long value) {
		this.id += value;
		updateRedisHashField(getKey(), "id", this.id);
	}

	public int getStrength() {
		return strength;
	}

	public void putStrength(int strength) {
		if (this.strength != strength) {
			this.strength = strength;
			updateRedisHashField(getKey(), "strength", this.strength);
		}
	}

	public void addStrength(int value) {
		this.strength += value;
		updateRedisHashField(getKey(), "strength", this.strength);
	}

	public long getRecoverTime() {
		return recoverTime;
	}

	public void putRecoverTime(long recoverTime) {
		if (this.recoverTime != recoverTime) {
			this.recoverTime = recoverTime;
			updateRedisHashField(getKey(), "recoverTime", this.recoverTime);
		}
	}

	public void addRecoverTime(long value) {
		this.recoverTime += value;
		updateRedisHashField(getKey(), "recoverTime", this.recoverTime);
	}

	public int getDateNum() {
		return dateNum;
	}

	public void putDateNum(int dateNum) {
		if (this.dateNum != dateNum) {
			this.dateNum = dateNum;
			updateRedisHashField(getKey(), "dateNum", this.dateNum);
		}
	}

	public void addDateNum(int value) {
		this.dateNum += value;
		updateRedisHashField(getKey(), "dateNum", this.dateNum);
	}

}